<template>
    <div>
        <breadcrumb></breadcrumb>
        <div class="box" v-loading="loading">
            <div class="title">{{ title }}</div>
            <div class="mainItem">
                <titleBg :title="'基本信息'"></titleBg>
                <el-form class="form" :model="Form" :rules="FormRules" ref="Form" label-width="100px">
                    <el-form-item label="优惠券类别" prop="couponType">
                        <el-radio-group v-model="Form.couponType" @input="changeCouponType">
                            <el-radio :label="3">满减券(货主端)</el-radio>
                            <el-radio :label="6">折扣券(货主端)</el-radio>
                            <el-radio :label="9">返点券(司机端)</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="优惠券类型" prop="couponName">
                        <el-select size="small" v-model="Form.couponName" placeholder="请选择" @change="couponModeltypes">
                            <!-- <el-option
                                v-if="Form.couponType == 9 ? item.value == 7 : Form.couponType == 6 ? item.value == 5 || item.value == 6 : item.value != 7 && item.value != 6 && item.value != 5"
                                v-for="item in couponResult" :key="item.value" :label="item.name" :value="item.value">
                            </el-option> -->
                            <template v-if="Form.couponType == 3">
                                <el-option v-for="item in couponResult3" :key="item.value" :label="item.name"
                                    :value="item.value">
                                </el-option>
                            </template>
                            <template v-else-if="Form.couponType == 6">
                                <el-option v-for="item in couponResult6" :key="item.value" :label="item.name"
                                    :value="item.value">
                                </el-option>
                            </template>
                            <template v-else-if="Form.couponType == 9">
                                <el-option v-for="item in couponResult9" :key="item.value" :label="item.name"
                                    :value="item.value">
                                </el-option>
                            </template>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="优惠券名称" prop="couponAlias">
                        <el-input class="inputStyle" size="small" v-model="Form.couponAlias"></el-input>
                    </el-form-item>
                    <el-form-item label="总数量" prop="total">
                        <el-input class="inputStyle" size="small" v-model="Form.total"
                            onkeyup="value=value.replace(/[^\d]/g,'')"></el-input>
                    </el-form-item>
                    <div v-if="Form.couponType == 6 && Form.couponName == 6">
                        <el-form-item label="折扣" class="redDian">
                            <el-form :model="discountListForm" ref="discountListForm">
                                <el-table :data="discountListForm.discountList" class="discountTable"
                                    :header-cell-style="tableHeaderStyle" :row-class-name="rowStyle">
                                    <el-table-column align="center" prop="serialNumber" label="序号" width="50">
                                        <template slot-scope="scope">
                                            <div>{{ scope.$index + 1 }}</div>
                                        </template>
                                    </el-table-column>
                                    <el-table-column align="center" label="区间运费" width="250">
                                        <template slot="header">
                                            <div class="beforeRedDian">区间运费
                                                <span class="iconfont_color2 icon-xiaoyudengyu"></span>
                                            </div>
                                        </template>
                                        <template slot-scope="scope">
                                            <div class="qujianStyle">
                                                <el-form-item :prop="'discountList.' + scope.$index + '.beginNumber'"
                                                    :rules="discountListRules.beginRules">
                                                    <el-input class="tableInputStyle" size="small"
                                                        v-model="discountListForm.discountList[scope.$index].beginNumber"
                                                        onkeyup="value=value.replace(/[^\d]/g,'')">
                                                        <template slot="suffix">
                                                            <div class="suffixText">元</div>
                                                        </template>
                                                    </el-input>
                                                </el-form-item>
                                                <span style="margin:0 10px">至</span>
                                                <el-form-item :prop="'discountList.' + scope.$index + '.endNumber'"
                                                    :rules="discountListRules.endRules">
                                                    <el-input class="tableInputStyle" size="small"
                                                        @input="endNumberInput($event, scope.$index)"
                                                        v-model="discountListForm.discountList[scope.$index].endNumber"
                                                        onkeyup="value=value.replace(/[^\d]/g,'')">
                                                        <template slot="suffix">
                                                            <div class="suffixText">元</div>
                                                        </template>
                                                    </el-input>
                                                </el-form-item>
                                            </div>
                                        </template>
                                    </el-table-column>
                                    <el-table-column align="center" label="折扣">
                                        <template slot="header">
                                            <div class="beforeRedDian">折扣</div>
                                        </template>
                                        <template slot-scope="scope">
                                            <el-form-item :prop="'discountList.' + scope.$index + '.discount'"
                                                :rules="discountListRules.discountRules">
                                                <el-input class="tableInputStyle" size="small"
                                                    v-model="discountListForm.discountList[scope.$index].discount"
                                                    onkeyup="value=value.replace(/[^\d.]/g,'')">
                                                    <template slot="suffix">
                                                        <div class="suffixText">%</div>
                                                    </template>
                                                </el-input>
                                            </el-form-item>
                                        </template>
                                    </el-table-column>

                                    <el-table-column align="center" label="操作">
                                        <template slot-scope="scope">
                                            <i class="iconfontcolor icon-a- commonStyle" v-show="scope.$index == 0"
                                                @click="addFreightScope()"></i>
                                            <i class="iconfontcolor icon-a-- commonStyle" v-show="scope.$index > 0"
                                                @click="delteFreightScope(scope.$index)"></i>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </el-form>
                        </el-form-item>
                    </div>
                    <div v-if="Form.couponType != 3 && Form.couponName != 6">
                        <el-form-item label="返点" prop="profitPoints">
                            <el-input class="inputStyle" size="small" v-model="Form.profitPoints">
                                <template slot="suffix">
                                    <div class="suffixText">%</div>
                                </template>
                            </el-input>
                        </el-form-item>
                    </div>

                    <el-form-item label="面额" prop="denomination" v-if="Form.couponType == 3 && Form.couponName != 5">
                        <el-input class="inputStyle" size="small" v-model="Form.denomination"
                            onkeyup="value=value.replace(/[^\d.]/g,'')">
                            <template slot="suffix">
                                <div class="suffixText">元</div>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="优惠条件" prop="ierms" v-if="Form.couponName != 6">
                        <el-radio-group v-model="Form.ierms">
                            <el-radio :label="3">无门槛使用</el-radio>
                            <el-radio :label="6">
                                满
                                <el-input size="small" style="width:136px" v-model="meetWith"
                                    onkeyup="value=value.replace(/[^\d]/g,'')"> </el-input>
                                元使用
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="适用场景" prop="scene">
                        <!--  当为指定车型券时  支持多选-->
                        <el-select size="small" v-model="Form.scene" placeholder="请选择" @focus="focusScene">
                            <el-option v-for="item in sceneDate[parseInt(Form.couponName) - 1]" :key="item.name"
                                :label="item.name" :value="item.name">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <!-- <el-form-item label="优惠说明" prop="couponType">
                        <el-input class="inputArea" resize="none" type="textarea" placeholder="请输入内容"
                            v-model="Form.illustrate" maxlength="100" show-word-limit>
                        </el-input>
                    </el-form-item> -->
                </el-form>
            </div>
            <div class="mainItem">
                <titleBg :title="'发放与领取设置'"></titleBg>
                <el-form class="form" :model="sendAndCollectForm" :rules="sendAndCollectFormRules" ref="sendAndCollectForm"
                    label-width="100px">
                    <el-form-item label="发放方式" prop="sendMethod">
                        <el-select size="small" v-model="sendAndCollectForm.sendMethod" placeholder="请选择">
                            <el-option v-for="item in sendMethodArr" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="发放条件" prop="sendCondition">
                        <el-radio-group v-model="sendAndCollectForm.sendCondition">
                            <el-radio :label="0">无门槛派送</el-radio>
                            <el-radio :label="1">
                                ⽤⼾每完成
                                <el-input size="small" style="width:80px" v-model="completeOrderNumber"
                                    onkeyup="value=value.replace(/[^\d]/g,'')"> </el-input>
                                单
                            </el-radio>
                            <el-radio :label="2">
                                ⽤⼾累计在线
                                <el-input size="small" style="width:80px" v-model="onlineTime"
                                    onkeyup="value=value.replace(/[^\d]/g,'')"> </el-input>
                                小时
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="有效期" prop="periodValidity">
                        <el-radio-group v-model="sendAndCollectForm.periodValidity">
                            <el-radio :label="0">
                                领取当日起
                                <el-input size="small" style="width:80px" v-model="todayStart"
                                    onkeyup="value=value.replace(/[^\d]/g,'')"> </el-input>
                                天
                            </el-radio>
                            <el-radio :label="1">
                                <el-date-picker v-model="dateTimeRange" type="datetimerange" align="right"
                                    :picker-options="pickerOptions" start-placeholder="开始日期" end-placeholder="结束日期"
                                    :default-time="['00:00:00', '00:00:00']" value-format="yyyy-MM-dd HH:mm:ss">
                                </el-date-picker>
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="每人限领" prop="limitPerPerson">
                        <el-input class="inputStyle" size="small" v-model="sendAndCollectForm.limitPerPerson"
                            onkeyup="value=value.replace(/[^\d]/g,'')">
                            <!-- /[^\d]/g 是一个正则表达式。
                                用于匹配所有不是数字的字符。它包含一个方括号表达式 [^\d]，表示匹配一个非数字字符(^ 在方括号内表示取反)。
                                另外，g 修饰符表示全局匹配，即在整个字符串中寻找所有匹配的字符。
-->
                        </el-input>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="footerControlBox">
            <el-button class="cancelBtn" @click="$router.push('/BasicSettings/vehicleConfig')">取消</el-button>
            <el-button class="submitBtn" type="primary" @click="saveData()">保存</el-button>
        </div>
    </div>
</template>

<script>
import titleBg from "@/components/titleBg.vue";
import breadcrumb from "@/components/breadcrumb.vue";
import httpApi from "@/http";

export default {
    data() {
        let couponAliasRules = (rule, value, callback) => {
            console.log(value);
            if (value == '') {
                callback(new Error('请输入'))
            } else if (value != '') {
                let params = {}
                if (this.title == '新建优惠券') {
                    params = {
                        couponAlias: value
                    }

                } else if (this.title == '编辑优惠券') {

                }

                httpApi.operationManagementApi.checkCouponAlias(params).then(res => {
                    if (res.data.code == 200) {
                        callback()
                    } else if (res.data.code == 202) {
                        callback(new Error('优惠券名称重复'))
                    }
                })

            }
        }
        // 优惠条件 
        let iermsRules = (rule, value, callback) => {
            if (value == 6) {//领取当日起多少天
                if (this.meetWith == '') {
                    callback(new Error('请输入'))
                } else if (!/^([1-9]\d*)$/.test(this.meetWith)) {
                    callback(new Error('请输入不为零的正整数'))
                } else {
                    callback()
                }

            } else if (value == 3) { //不需要正则验证 直接通过
                callback()
            }

        }
        // 有效期
        let periodValidityRules = (rule, value, callback) => {

            if (value == 0) {//领取当日起多少天
                if (this.todayStart == '') {
                    callback(new Error('请输入'))
                } else if (!/^([1-9]\d*)$/.test(this.todayStart)) {
                    callback(new Error('天数格式不正确'))
                } else {
                    callback()
                }

            } else if (value == 1) {//不需要正则验证 直接通过
                callback()
            }
        }
        //发放条件
        let sendConditionRules = (rule, value, callback) => {
            console.log(value);
            if (value == 1) {//用户每完成多少单
                if (this.completeOrderNumber == '') {
                    callback(new Error('请输入'))
                } else if (!/^([1-9]\d*)$/.test(this.completeOrderNumber)) {
                    callback(new Error('完成单数格式不正确'))
                } else {
                    callback()
                }

            } else if (value == 2) {//用户在线多少小时
                if (this.onlineTime == '') {
                    callback(new Error('请输入'))
                } else if (!/^([1-9]\d*)$/.test(this.onlineTime)) {
                    callback(new Error('在线小时格式不正确'))
                } else {
                    callback()
                }
            } else if (value == 0) {
                callback()
            }
        }

        let beginNumberRules = (rule, value, callback) => {
            console.log(rule);

            let regular = /^([1-9]\d*|0)$/
            if (value == '') {
                callback(new Error('区间运费不能为空'))
            }
            else if (!regular.test(value)) {
                callback(new Error('运费金额格式错误'))
            } else {
                callback()
            }
        };
        let endNumberRules = (rule, value, callback) => {
            let regular = /^([1-9]\d*)$/
            if (value == '') {
                callback(new Error('区间运费不能为空'))
            }
            else if (!regular.test(value)) {
                callback(new Error('运费金额格式错误'))
            } else {
                callback()
            }
        };
        let discountRateRules = (rule, value, callback) => {
            let regular = /^([1-9]\d*(\.\d*[1-9][0-9])?)|^(0\.\d*[1-9][0-9])$|^(0\.\d*[1-9])$/

            if (value == '') {
                callback(new Error('折扣不能为空'))
            } else if (value.split('.').length > 2) {
                callback('只能有一个小数点')
            } else if (!regular.test(value)) {
                callback(new Error('请输入正确折扣百分比'))
            } else if (value > 100) {
                callback(new Error('折扣不能超过100%'))
            } else {
                callback()
            }
        };


        let denominationRules = (rule, value, callback) => {
            //  (\.\d*)? //表示跟小数点，以及任意数字：匹配小数点，以及小数点后面可以为任意数
            //  | //表示选择，即“或”，第二种可能
            //  0\.\d*[1-9][0-9] //表示以0开头，后面接小数点，小数点后面第一位数在1~9之间，第二位数可以在0~9之间
            //  | //表示选择，即“或”，第三种可能
            //  0\.\d*[1-9] 表示以0开头，后面接小数点，小数点后面只有一位不为0的数
            // /        
            let regular = /^([1-9]\d*(\.\d*[1-9][0-9])?)|^(0\.\d*[1-9][0-9])$|^(0\.\d*[1-9])$/

            if (value == '') {
                callback(new Error('面额金额不能为空'))
            } else if (value.split('.').length > 2) {
                callback('只能有一个小数点')
            } else if (!regular.test(value)) {
                callback(new Error('请输入正确面额金额'))
            } else {
                callback()
            }
        };
        let profitPointsRules = (rule, value, callback) => {

            let regular = /^([1-9]\d*(\.\d*[1-9][0-9])?)|^(0\.\d*[1-9][0-9])$|^(0\.\d*[1-9])$/

            if (value == '') {
                callback(new Error('返点不能为空'))
            } else if (value.split('.').length > 2) {
                callback('只能有一个小数点')
            } else if (!regular.test(value)) {
                callback(new Error('请输入正确返点百分比'))
            } else if (value > 100) {
                callback(new Error('返点不能超过100%'))
            } else {
                callback()
            }
        };


        return {
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            title: "新建优惠券",
            // 基本信息表单
            Form: {
                couponType: 3, //优惠券类别
                total: "", //总量
                couponName: "", //优惠券类型名
                denomination: "", //面额
                scene: "", //适用场景
                ierms: "", //优惠条件
                illustrate: "",//优惠说明
                profitPoints: "",//返点
                couponAlias: "",//优惠券名称
            },
            //基本信息表单 验证规则
            FormRules: {
                couponType: [{ required: true, message: "请选择", trigger: "change" }], //优惠券类别
                total: [{ required: true, message: "请输入", trigger: "blur" }, {
                    pattern: /^([1-9]\d*|0)$/,
                    message: "总数量格式不正确",
                    trigger: "blur",
                },], //总量 
                couponName: [{ required: true, message: "请选择", trigger: "change" }], //优惠券类型名

                scene: [{ required: true, message: "请选择", trigger: "change" }], //适用场景
                ierms: [{ required: true, message: "请选择", trigger: "change" }, {
                    validator: iermsRules,
                    trigger: "change",
                }], //优惠条件
                illustrate: [{ message: "请输入", trigger: "blur" }],//优惠说明
                denomination: [{ required: true, message: "请输入", trigger: "blur" }, {
                    validator: denominationRules,
                    trigger: "blur",
                }],
                profitPoints: [{ required: true, message: "请输入", trigger: "blur" }, {
                    validator: profitPointsRules,
                    trigger: "blur",
                }],
                couponAlias: [{
                    required: true,
                    validator: couponAliasRules,
                    trigger: "blur",
                }]
            },
            discountListForm: {
                discountList: [{ beginNumber: "", endNumber: "", discount: "" }]
            },


            discountListRules: {
                beginRules: [{
                    validator: beginNumberRules,
                    trigger: 'blur',
                }],
                endRules: [{
                    validator: endNumberRules,
                    trigger: 'blur',
                }],
                discountRules: [{
                    validator: discountRateRules,
                    trigger: 'blur',
                }]
            },


            //发放与领取设置表单
            sendAndCollectForm: {
                sendCondition: "",//发放条件
                sendMethod: "",//发放方式
                periodValidity: "",//有效期
                limitPerPerson: "",//每人限领
            },
            ////发放与领取设置表单规则
            sendAndCollectFormRules: {
                sendCondition: [{ required: true, message: "请选择", trigger: "change" }, {
                    validator: sendConditionRules,
                    trigger: "change",
                }],//发放条件
                sendMethod: [{ required: true, message: "请选择", trigger: "change" }],//发放方式
                periodValidity: [{ required: true, message: "请选择", trigger: "change" }, {
                    validator: periodValidityRules,
                    trigger: "change",
                }],//有效期
                limitPerPerson: [{ required: true, message: "请输入", trigger: "blur" }, {
                    pattern: /^([1-9]\d*|0)$/,
                    message: "每人限领格式不正确",
                    trigger: "blur",
                }],//每人限领
            },
            sceneDate: [
                [{ name: "通用" }], //无门槛抵扣 适应场景选项
                [{ name: "长途大车(5米2以上大车)等" }],     //指定车型券适应场景选项
                [{ name: "搬运服务" }],//搬家券 适应场景选项
                [{ name: "通用" }],  //货运券  适应场景选项
                [{ name: "运费原价百分比" }],   //折扣券  适应场景选项
                [{ name: "按运费区间折扣" }],   //阶梯折扣券 适应场景选项
                [{ name: "司机运费按百分比返点" }]  //返点券 适应场景选项
            ],

            sceneArr: [
            ],
            meetWith: '',
            resultArray: [
                "NOTHRESHOLDDEDUCTION",  //无门槛抵扣
                "DESIGNATEDVEHICLEMODELCOUPON",//指定车型券
                "MOVINGVOUCHER",//搬家券
                "FREIGHTVOUCHER",//货运券
                "COUPON",//折扣券
                "STEPDISCOUNTCOUPON",//阶梯式折扣券
                "DRIVERREBATECOUPON"//司机返点券
            ],
            //满减券(货主端) 优惠券类型可选
            couponResult3: [{
                value: 1,
                name: '无门槛抵扣',
            },
            {
                value: 2,
                name: '指定车型券',
            },
            {
                value: 3,
                name: '搬家券',
            },
            {
                value: 4,
                name: '货运券',
            }],
            //折扣券(货主端) 优惠券类型可选
            couponResult6: [{
                value: 5,
                name: '折扣券',
            },
            {
                value: 6,
                name: '阶梯式折扣券',
            }],
            //返点券 司机端
            couponResult9: [{
                value: 7,
                name: '司机返点券',
            }],

            loading:false,
            sendMethodArr: [{ label: '系统推送', value: 0 }],
            dateTimeRange: "",
            todayStart: "", //今日领取起 x天
            morrowStart: "",//明日领取起 x天
            completeOrderNumber: "",//用户每完成单数
            onlineTime: "",

            isDiscountTrue: true,
            msgError: "",


        };
    },
    methods: {
        tableHeaderStyle({ row, column, rowIndex, columnIndex }) {
            return 'font-size:12px;fontWweight:bold;color:#262626;backgroundColor:#F5F5F5;borderColor:#DDDDDD;height:40px;paddingTop:0px;paddingBottom:0px;'
        },
        rowStyle({ row, rowIndex }) {
            return 'tableRowStyleName'
        },

        changeCouponType(val) {

        },
        delteFreightScope(index) {
            this.discountListForm.discountList.splice(index, 1);
        },
        addFreightScope() {

            let defaulItem = { beginNumber: "", endNumber: "", discount: "" }

            this.discountListForm.discountList.push(JSON.parse(JSON.stringify(defaulItem)))



        },
        endNumberInput(val, index) {
            console.log(val, index);
            if (index + 1 != this.discountListForm.discountList.length) {
                this.discountListForm.discountList[index + 1].beginNumber = val
            }

        },
        proingDiscount() {
            //后端区间含等于 例如 100<=200 200<=300 300以上
            console.log('proingDiscount顺序1')
            this.$refs['discountListForm'].validate((valid) => {
                if (valid) {//判断表单是否填写且通过正则规则

                    // 优惠券类型为折扣券 且为阶梯式折扣券  判断区间运费 折扣 是否为从小到大 排序
                    try {

                        this.discountListForm.discountList.forEach((item, index) => {
                            if (index + 1 != this.discountListForm.discountList.length) {
                                let nextItem = this.discountListForm.discountList[index + 1]
                                if (Number(item.discount) > Number(nextItem.discount)) {//区间1折扣率  大于 区间2 折扣 则报错

                                    // this.$message.error('折扣率请根据区间从小到大填写')
                                    console.log('折扣率请根据区间从小到大填写');
                                    this.isDiscountTrue = false
                                    throw new Error("折扣率请根据区间从小到大填写");
                                }

                                if (Number(item.endNumber) != Number(nextItem.beginNumber)) {//一区间输入框结束值 ==二区间输入框起始值  才连续
                                    // this.$message.error('阶梯式优惠券的折扣区间运费需连续')
                                    console.log('阶梯式优惠券的折扣区间运费需连续');
                                    this.isDiscountTrue = false
                                    throw new Error("阶梯式优惠券的折扣区间运费需连续");
                                }
                            }

                            if (Number(item.beginNumber) > Number(item.endNumber) || Number(item.beginNumber) == Number(item.endNumber)) {
                                // this.$message.error('请从小到大填写区间运费')
                                console.log('请从小到大填写区间运费');
                                this.isDiscountTrue = false
                                throw new Error("请从小到大填写区间运费");
                            }

                        })


                        this.isDiscountTrue = true

                    } catch (e) {
                        this.$message.error(e)
                        console.log(e);
                    }




                } else {
                    this.$message.error('请填写完整信息')
                    this.isDiscountTrue = false
                    return false;
                }
            })

        },
        saveData() {
            if(this.loading){//loading为true代表正在加载 保存中,防止重复点击
                return
            }
            this.loading=true
            // 选择了阶梯折扣券 需要验证阶梯区间规则
            if (this.Form.couponType == 6 && this.Form.couponName == 6) {
                this.proingDiscount()
                //   isDiscountTrue为True代表通过验证 
                if (!this.isDiscountTrue) {//false未通过 就不执行后续代码
                    return
                }

            }


            var d = {
                couponPort: parseInt(this.Form.couponType) == 9 ? 'driver' : 'cargoowner',  //driver司机端/cargoowner货主端
                vehicleModel: this.Form.couponName == 2 ? "长途大车(5米2以上大车)等" : "", //车型限制

                couponTotal: this.Form.total,  //优惠券总数数量默认为0，为0时不限制数量
                couponNumber: this.sendAndCollectForm.limitPerPerson,//司机限制领取数量
                sendType: this.sendAndCollectForm.sendMethod, //0为系统推送,默认为0,1为指定司机
                couponSend: this.sendAndCollectForm.sendCondition, //发送方式0推送对应平台全部1,用户累计在线时长2,司机完成单数
                numericalValue: this.sendAndCollectForm.sendCondition == 1 ? this.completeOrderNumber : this.sendAndCollectForm.sendCondition == 2 ? this.onlineTime : 0,
                // 发放条件 //0无门槛 1在线时长  2司机完成单数的数值
                timeType: this.sendAndCollectForm.periodValidity,
                //有效期 0领取后开始计算失效时间，1为优惠券设置使用时间范围
                couponUser: ""//指定用户
            }

            if (this.sendAndCollectForm.periodValidity == 0) {//timeType为0  领取当日起 this.todayStart天
                d.timeNumber = this.todayStart
            } else {//timeType为1  有效日期 范围 
                d.beginTime = this.dateTimeRange[0]
                d.endTime = this.dateTimeRange[1]
            }
            var k = {
                couponAlias: this.Form.couponAlias,//优惠券名称
                couponType: this.Form.couponType == 3 ? '满减券' : this.Form.couponType == 6 ? '折扣券' : '返点券',
                couponName: this.resultArray[parseInt(this.Form.couponName) - 1],
                couponScene: this.Form.scene, //适用场景
                meetWith: this.Form.ierms == 3 ? '' : this.meetWith, //满足条件
                reduction: this.Form.couponType == 3 ? this.Form.denomination : this.Form.profitPoints, //减免值   
                list: this.discountListForm.discountList //优惠券若是阶梯类型，阶梯列表
            }

            var result = {
                couponRequest: d,
                couponSubtableRequest: k
            }

            this.$refs['Form'].validate((valid1) => {

                if (valid1) {
                    this.$refs['sendAndCollectForm'].validate((valid2) => {

                        if (valid2) {

                            httpApi.operationManagementApi.addCoupon(result).then(res => {
                                if (res.data.code == 200) {
                                    this.$message.success('新增优惠券成功')
                                    this.$router.push('/operationManagement/coupon')
                                }
                                this.loading=false
                            })

                        } else {
                            this.$message.error('请填写完整信息')
                            this.loading=false
                            return false;
                        }
                    });

                } else {
                    console.log('填写完整信息');
                    this.$message.error('请填写完整信息')
                    this.loading=false
                    return false;
                }
            });
        },
        // 优惠券类型改变时,
        couponModeltypes(val) {
            console.log(val);
            //清空业务场景绑定选项值
            this.Form.scene = ''
        },
        focusScene(val) {
            if (this.Form.couponName == '') {
                this.$message.warning('请先选择优惠券类型')
            }

        },
        //获取指定车型券 适用场景选项
        getCarTypeData() {
            httpApi.operationManagementApi.getCarType().then(res => {
                if (res.data.code == 200) {
                    this.sceneDate.splice(1, 1, res.data.data)
                    console.log(this.sceneDate);
                }
            })
        }
    },
    mounted() {
        this.getCarTypeData()

    },
    components: {
        titleBg,
        breadcrumb,
    },
};
</script>

<style lang="scss" scoped>
.box {
    background-color: #fff;
    height: calc(100vh - 201px);
    overflow-y: scroll;



    .title {
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: bold;
        color: #262626;
        height: 51px;
        line-height: 50px;
        border-bottom: 1px solid #f0f0f0;
        margin-left: 23px;
    }

    .mainItem {
        padding: 34px 24px 24px 56px;
    }

    .form {
        .icon-xiaoyudengyu {
            font-size: 12px;
        }

        ::v-deep .el-input__suffix {
            color: #1890FF;
        }

        .redDian {
            ::v-deep .el-form-item__label:before {
                content: "*";
                color: #f56c6c;
                margin-right: 4px;
            }
        }

        .beforeRedDian:before {
            content: "*";
            color: #f56c6c;
            margin-right: 4px;
        }

 
        .discountTable {
            width: 100%;

            ::v-deep .el-form-item__error {
                z-index: 10000;
                position: absolute;
                top: 70%;
            }

            ::v-deep .el-form-item__content {
                height: 80px;
                display: flex;
                align-items: center;
            }

            .qujianStyle {
                display: flex;
                align-items: center;
            }

            .tableInputStyle {


                .suffixText {
                    font-size: 12px;
                    font-family: PingFang SC;
                    font-weight: 400;
                    color: #CCCCCC;
                    text-align: center;
                    line-height: 40px;

                    padding-right: 10px;
                }
            }

            .suffixText {
                font-size: 12px;
                font-family: PingFang SC;
                font-weight: 400;
                color: #CCCCCC;
                text-align: center;
                line-height: 32px;
                padding-right: 10px;
            }





            .commonStyle {
                font-size: 19px;
                cursor: pointer;
                transition: 0.2s;

                &:hover {
                    opacity: 0.8;
                }
            }
        }

        .inputStyle {
            width: 420px;

            .suffixText {
                font-size: 12px;
                font-family: PingFang SC;
                font-weight: 400;
                color: #CCCCCC;
                text-align: center;
                padding-right: 10px;
            }
        }

        ::v-deep .el-form-item__label {
            font-size: 12px;
            font-family: Microsoft YaHei;
            font-weight: 600;
            color: #262626
        }

        ::v-deep .el-radio {
            font-size: 12px;
            font-family: PingFang SC;
            font-weight: 400;
            color: #262626;
        }

        .inputArea {
            width: 616px;
            background: #FFFFFF;
            border: 1px solid #F0F0F0;
            border-radius: 4px
        }
    }
}

.footerControlBox {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 60px;
    background-color: #fff;
    margin-top: 14px;

    .cancelBtn {
        width: 80px;
        height: 36px;
        border: 1px solid #d9d9d9;
        color: #262626;
        font-size: 12px;
        border-radius: 4px;
        background-color: #fff;
        margin-right: 16px;
        transition: 0.2;

        &:hover {
            color: #1890ff;
            border-color: #1890ff;

        }
    }

    .submitBtn {
        background-color: #1890ff;
        width: 80px;
        height: 36px;
        color: #fff;
        border-radius: 4px;
        font-size: 12px;
    }

    &:hover {
        opacity: 0.8;
        transition: 0.2;
    }
}
</style>

<style lang = "scss">
.tableRowStyleName {
    height: 80px;
    color: #262626 !important;
    font-size: 12px;
    padding: 20px 0 !important;

}
</style>
